package io.kiki.sba.registry.store.jdbc.repository.impl;


import io.kiki.sba.registry.common.model.store.StringInterner;
import io.kiki.sba.registry.store.jdbc.domain.AppRevisionDomain;
import io.kiki.sba.registry.store.jdbc.informer.DbEntryContainer;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

public class AppRevisionContainer implements DbEntryContainer<AppRevisionDomain> {
    private final Map<String, String> data = new ConcurrentHashMap<>();

    @Override
    public synchronized void onEntry(AppRevisionDomain entry) {
        String revision = StringInterner.intern(entry.getRevision());
        String appName = StringInterner.intern(entry.getAppName());
        if (entry.isDeleted()) {
            data.remove(revision);
        } else {
            data.put(revision, appName);
        }
    }

    public boolean containsRevisionId(String revisionId) {
        return data.containsKey(revisionId);
    }

    public int size() {
        return data.size();
    }

    public Set<String> allRevisionIds() {
        return data.keySet();
    }

    public void foreach(BiConsumer<String, String> f) {
        data.forEach(f);
    }
}
